/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util;

/**
 *
 * @author bennyl
 */
public class IPs {

    public static String decodeIp(long encodedIp) {
        long[] ip = {0, 0, 0, 0};
        for (int i = 0; i < ip.length; i++) {
            encodedIp = (encodedIp - (ip[ip.length - i - 1] = encodedIp % 256)) / 256;
        }

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < ip.length; i++) {
            result.append(".").append(ip[i]);
        }

        return result.substring(1);
    }

    public static long encodeIp(String ip) {
        String[] parts = ip.split("\\.");
        if (parts.length != 4) {
            return -1L;
        }
        long result = 0L;
        for (int i = 0; i < 4; i++) {
            result = result * 256 + Integer.parseInt(parts[i]);
        }

        return result;
    }
}
